<?php

declare (strict_types=1);

namespace app\store\controller\shop;

use app\store\controller\Controller;
use think\response\Json;
use cores\exception\BaseException;
use app\store\model\shop\Category as ShopCategoryModel;

/**
 * 商户主营行业控制器
 * Class Goods
 * @package app\shop\controller
 */
class Category extends Controller
{
    /**
     * 商户主营行业列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        // 获取列表记录
        $model = new ShopCategoryModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }



    /**
     * 商户主营行业详情(详细信息)
     * @param int $categoryId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $categoryId): Json
    {
        // 获取商户主营行业详情
        $model = new ShopCategoryModel;
        $categoryInfo = $model->detail($categoryId);
        return $this->renderSuccess(compact('categoryInfo'));
    }



    /**
     * 添加商户主营行业
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(): Json
    {
        $model = new ShopCategoryModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 商户主营行业编辑
     * @param int $categoryId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(int $categoryId): Json
    {
        // 商户主营行业详情
        $model = ShopCategoryModel::detail($categoryId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

   

    /**
     * 删除商户主营行业
     * @param array $categoryIds
     * @return Json
     */
    public function delete(int $categoryId): Json
    {
        // 优惠券详情
        $model = ShopCategoryModel::detail($categoryId);
        // 更新记录
        if ($model->setDelete()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除成功');
    }
}
